/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Shared/DB/SqlOperation.h"
#include "Shared/DB/SqlPreparedStatement.h"
#include "Shared/DB/SqlConnection.h"


namespace Shared {
namespace DB {

    SqlPreparedRequest::SqlPreparedRequest(int nIndex, SqlStmtParameters* arg) 
        : _index(nIndex), _param(arg)
    {
    }

    SqlPreparedRequest::~SqlPreparedRequest()
    {
        delete _param;
    }

    bool SqlPreparedRequest::execute(SqlConnection* conn)
    {
        SqlConnection::Lock guard(conn);
        return conn->executeStmt(_index, *_param);
    }

    void SqlResultQueue::update()
    {
        IQueryCallback* callback = NULL;
        while (next(callback))
        {
            callback->execute();
            delete callback;
        }
    }

    bool SqlQuery::execute(SqlConnection* conn)
    {
        if (!_callback || !_queue)
        { return false; }

        SqlConnection::Lock guard(conn);
        _callback->setResult(conn->query(_sql));
        _queue->add(_callback);
        return true;
    }

}}